/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.kj.service;

import java.math.BigDecimal;
import java.util.List;

import com.jeesite.modules.kj.api.enumCode.UnicomResponseEnums;
import com.jeesite.modules.kj.api.exception.UnicomRuntimeException;
import com.jeesite.modules.kj.exception.LowIntegralException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.kj.entity.KjMemberIntegrals;
import com.jeesite.modules.kj.dao.KjMemberIntegralsDao;

/**
 * kj_member_integralsService
 * @author hzy
 * @version 2021-07-04
 */
@Service
@Transactional(readOnly=true)
public class KjMemberIntegralsService extends CrudService<KjMemberIntegralsDao, KjMemberIntegrals> {

	/**
	 * 获取单条数据
	 * @param kjMemberIntegrals
	 * @return
	 */
	@Override
	public KjMemberIntegrals get(KjMemberIntegrals kjMemberIntegrals) {
		return super.get(kjMemberIntegrals);
	}

	/**
	 * 查询分页数据
	 * @param kjMemberIntegrals 查询条件
	 * @param kjMemberIntegrals.page 分页对象
	 * @return
	 */
	@Override
	public Page<KjMemberIntegrals> findPage(KjMemberIntegrals kjMemberIntegrals) {
		return super.findPage(kjMemberIntegrals);
	}

	/**
	 * 保存数据（插入或更新）
	 * @param kjMemberIntegrals
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(KjMemberIntegrals kjMemberIntegrals) {
		super.save(kjMemberIntegrals);
	}

	/**
	 * 更新状态
	 * @param kjMemberIntegrals
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(KjMemberIntegrals kjMemberIntegrals) {
		super.updateStatus(kjMemberIntegrals);
	}

	/**
	 * 删除数据
	 * @param kjMemberIntegrals
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(KjMemberIntegrals kjMemberIntegrals) {
		super.delete(kjMemberIntegrals);
	}

	@Transactional(readOnly=false)
	public KjMemberIntegrals findByMemberId(String memberId){
		KjMemberIntegrals where = new KjMemberIntegrals();
		where.setMemberId(memberId);
		List<KjMemberIntegrals> list = findList(where);
		if(list.size() == 0){
			KjMemberIntegrals mi = new KjMemberIntegrals();
			mi.setMemberId(memberId);
			mi.setTotalIntegrals(BigDecimal.ZERO);
			mi.setUsedIntegrals(BigDecimal.ZERO);
			mi.setTotalHours(BigDecimal.ZERO);
			save(mi);
			return mi;
		}else{
			return list.get(0);
		}
	}


	//获取剩余积分
	public BigDecimal getLastIntegrals(String memberId){
		KjMemberIntegrals ki = findByMemberId(memberId);
		return ki.getTotalIntegrals().subtract(ki.getUsedIntegrals());
	}

	@Transactional(readOnly=false)
	public void addIntegrals(String memberId, BigDecimal integrals, BigDecimal hours){
		KjMemberIntegrals ki = findByMemberId(memberId);
		if(integrals.compareTo(new BigDecimal("0")) < 0){
			ki.setUsedIntegrals(ki.getUsedIntegrals().subtract(integrals));
		}else {
			ki.setTotalIntegrals(ki.getTotalIntegrals().add(integrals));
		}
		ki.setTotalHours(ki.getTotalHours().add(hours));
		ki.setIsNewRecord(false);
		save(ki);
	}
	@Transactional(readOnly=false)
	public void subtractIntegrals(String memberId, BigDecimal integrals) throws LowIntegralException {
		KjMemberIntegrals ki = findByMemberId(memberId);
		BigDecimal lastIntegrals = getLastIntegrals(memberId);
		if(lastIntegrals.compareTo(integrals) >= 0){
			ki.setUsedIntegrals(ki.getUsedIntegrals().add(integrals));
			save(ki);
		}else{
			throw new UnicomRuntimeException(UnicomResponseEnums.FAIL_GETDATA, "积分不足以扣减");
		}
	}


	public BigDecimal getTotalIntegrals(){
		return dao.getTotalIntegrals();
	}


	public BigDecimal getTotalHours(){
		return dao.getTotalHours();
	}

	public List<String> getHasIntegralsMobile(){
		return dao.getHasIntegralsMobile();
	}


	public List<String> getNoIntegralsMobile(){
		return dao.getNoIntegralsMobile();
	}

	@Transactional(readOnly = false)
	public void clearIntegrals(KjMemberIntegrals integrals){
		try {
			subtractIntegrals(integrals.getMemberId(), BigDecimal.ONE);
		} catch (LowIntegralException e) {
			e.printStackTrace();
		}
	}
}
